package com.api.edc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.api.edc.model.ApiConstant;
import com.api.edc.model.LoginResult;
import com.nova.edc.model.system.Login;
import com.seio.edc.common.MD5;

@Controller
public class LoginAPIController {

	@Autowired
	private com.nova.edc.service.system.LoginService novaLoginService;

	@RequestMapping(value = "appLogin.shtml")
	@ResponseBody
	public LoginResult userNameVali(HttpServletRequest request,
			HttpSession session, String userName, String password) {
		LoginResult loginResult = new LoginResult();
		List<Login> list = new ArrayList<Login>();
		try {
			password = (password == null ? "" : MD5.Md5(request
					.getParameter("password"))); // 加密完的进行匹配
			Login login = novaLoginService.isExistLogin(userName, password);
			if (login != null) {
				loginResult.setCode(ApiConstant.CODE_SUCCESS);
				loginResult.setMessage(ApiConstant.MESSAGE_LOGIN_SUCCESS);
				list.add(login);
				session.setAttribute(ApiConstant.APP_SESSION_LOGIN, login);
			} else {
				loginResult.setCode(ApiConstant.CODE_FAILUER);
				loginResult.setMessage(ApiConstant.MESSAGE_LOGIN_FAILUER);
			}
		} catch (Exception e) {
			loginResult.setCode(ApiConstant.CODE_EXCEPTION);
			loginResult.setMessage(ApiConstant.MESSAGE_EXCEPTION);
		}
		loginResult.setData(list);
		return loginResult;
	}
}
